package Cliente;

import Interface.Jogo;
import java.awt.event.*;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.swing.*;

/**
 *
 * @author Pen²
 */
public class StopWatch{
    
	
	JLabel promptLabel, timerLabel;
	int counter;
	Timer timer;
        int min = 3;        
        int segundos = 60;
        
	
	public StopWatch(JLabel timeLabel) throws UnknownHostException, IOException, ClassNotFoundException{
	
		this.timerLabel = timeLabel;
                //letra=new Letras();
                
                String sec = "180.00";
		int count = (int)(Double.parseDouble(sec));
                
		this.timerLabel.setText("Faltam: " + min + ":00 s");
                min = (count/60)-1;
		TimeClass tc = new TimeClass(count,this.timerLabel);
		timer = new Timer(1000,tc);
		timer.start();
	}

public class TimeClass implements ActionListener {
	int counter;
	JLabel jl;
	public TimeClass(int counter,JLabel l)
        {
		this.counter = counter;
                this.jl = l;
	}

	public void actionPerformed(ActionEvent tc)
        {
                segundos--;
		counter --;
                
		if(counter >= 1){
                    if(segundos==0){
                        min--;
                        if(min==0){
                            segundos=60;
                            jl.setText("Faltam: " + min +  ":" + segundos + " s");
                        }else{
                            segundos=60;
                            jl.setText("Faltam: " + min +  ":00 s");
                        }
                    }
                    else
                        if(segundos<10&&segundos>0)
                            jl.setText("Faltam: " + min + ":" + "0" + segundos + " s");
                        else
                            jl.setText("Faltam: " + min + ":" + segundos + " s");
		}else{
			timer.stop();
                  
			jl.setText("Processando Pontuação.........");
                        try {  
                            File soundFile = new File("src/music/gameover.wav");  
                            AudioInputStream sound = AudioSystem.getAudioInputStream(soundFile);  
                            DataLine.Info info = new DataLine.Info(Clip.class, sound.getFormat());  
                            Clip clip = (Clip) AudioSystem.getLine(info);  
                            clip.open(sound);  
                            clip.start();
                            System.out.println("chegou aquiiiii classe stopwath");
                            
                            Letras.EnviarPalavrasServidor();
                            
                            
                            
                            //jogo.resultadodoJogo();
                            
                            System.out.println(" passou aqui classe stopwath");
                        } catch (Exception e) {
                            Toolkit.getDefaultToolkit().beep();
//                            JOptionPane.showMessageDialog(this, e);  
                        }  
			
		}
	}
    }

//    public void timer(){
//                String sec = "180.00";
//		int count = (int)(Double.parseDouble(sec));
//		timerLabel.setText("Time left:" + count);
//
//		TimeClass tc = new TimeClass(count);
//		timer = new Timer(1000,tc);
//		timer.start();
//    }
}

